package com.gingko.duominuo.utils

import com.gingko.duominuo.App
import com.gingko.duominuo.data.model.NameMappingVo
import com.gingko.duominuo.data.source.local.RoomModule
import kotlinx.coroutines.runBlocking

object NameMappingUtils {
    private val projects = mutableListOf<NameMappingVo>()
    private val platforms = mutableListOf<NameMappingVo>()
    private val tasks = mutableListOf<NameMappingVo>()

    fun initData() {
        projects.clear()
        platforms.clear()
        tasks.clear()
        runBlocking {
            val appDataBase = RoomModule.provideAppDataBase(App.instance)
            val nameMappingDao = RoomModule.providerNameMappingDao(appDataBase)
            projects.addAll(nameMappingDao.queryNameMappingByGroup("项目") ?: listOf())
            platforms.addAll(nameMappingDao.queryNameMappingByGroup("平台") ?: listOf())
            tasks.addAll(nameMappingDao.queryNameMappingByGroup("任务") ?: listOf())
        }
    }


    fun getProjectName(key: String?): String? {
        return projects.firstOrNull { it.name == key }?.desc ?: key
    }

    fun getPlatformName(key: String?): String? {
        return platforms.firstOrNull { it.name == key }?.desc ?: key
    }

    fun getTaskName(key: String?): String? {
        return tasks.firstOrNull { it.name == key }?.desc ?: key
    }
}